var comments = Class.create();
Object.extend(comments, {
	app_comment:function(app,control,act,id,res_uid,code){
		var request_url = '/~comment.index.view_comment';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters : $H({app:app,control:control,act:act,id:id,res_uid:res_uid,code:code}),
				asynchronous: true,
				onComplete : function (httpReq){
					//alert(httpReq.responseText);
					var comment_id = app+'_'+control+'_'+act+'_'+id;
					$(comment_id).innerHTML = httpReq.responseText;
					//$(comment_id).innerHTML = document.write(httpReq.responseText);
				}
			});
	},
	refush_page: function(url){
		window.location.href=url;
	},
	remove_comment: function(thread_id){
		if(!confirm('你确认要删除这条评论和它的所有回复吗？')){   return false; }
		var request_url ='/~comment.index.delete_comment';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post', 
				parameters :'thread_id='+thread_id, 
				asynchronous: true,
				onComplete : function (transport){						
						var ret = transport.responseText.evalJSON();				
						this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id']);				
				}.bindAsEventListener(this)
			});
	},
	remove_comment_post: function(thread_id,post_id){
		if(!confirm('您确认要删除此回复吗？')){   return false; } //alert(post_id);
		var request_url ='/~comment.index.delete_comment_post';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters :$H({post_id:post_id,thread_id:thread_id}),
				asynchronous: true,
				onComplete : function (transport){						
						var ret = transport.responseText.evalJSON();				
						this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id']);					
					
				}.bindAsEventListener(this)
			});
	},	
	remove_comment_list: function(thread_id){
		if(!confirm('您确认要删除此评论吗？')){   return false; }
		var request_url ='/~comment.index.delete_comment';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters :'thread_id='+thread_id,
				asynchronous: true,
				onComplete : function (){	window.location.reload();	}
			});
	},
	reply_comment:function(form_id){
		var check = this.comment_submit_check(form_id); if(!check){ return false;}
		var form_obj = $(form_id);
		var pars = form_obj.serialize();
		form_obj.disable();
		var request_url = '/~comment.index.reply_comment';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters :pars,
				asynchronous: true,
				onComplete : function (transport){

						var ret = transport.responseText.evalJSON();
						this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id']);

				}.bindAsEventListener(this)
			});
	},
	reply_comment_list:function(form_id){
		var check = this.comment_submit_check(form_id); if(!check){ return false;}
		var form_obj = $(form_id);
		var pars = form_obj.serialize();
		var request_url = '/~comment.index.reply_comment';
		var myAjax 	= new Ajax.Request(request_url,{
				method : 'post',
				parameters :pars,
				asynchronous: true,
				onComplete : function (){  window.location.reload(); }
			});
	},
	comment_submit_check: function(form_id){
		var form_obj = $(form_id);
		if(this.comment_trim(form_obj.content.value).length == 0  ){
				$(form_id+'_msg').innerHTML = "<span   class='ufriend-tip'><span class='e_icon'></span>您说什么？</span>";
				return 0;
		}
		if(this.comment_trim(form_obj.content.value).length >100) {
			$(form_id+'_msg').innerHTML = "<span  class='ufriend-tip'><span class='e_icon'></span> 超出字数限制！ </span>";
			return 0;
		}else  return 1;
	},
	comment_trim:function (str) {
		return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
	},
	
	add_comment: function(form_id){ //alert(form_id);
		var check = this.comment_submit_check(form_id); //alert(check);   
		if(!check){ return false;}
		var form_obj=$(form_id);
		var pars=form_obj.serialize();
		form_obj.disable();
		var request_url ='/~comment.index.send_comment';
		var myAjax = new Ajax.Request(request_url,{
				method : 'post',
				parameters : pars,
				asynchronous: true,
				onComplete : function (transport){
					//alert(transport); alert(1);
					var ret = transport.responseText.evalJSON();
					//alert(ret);
					this.app_comment(ret['data']['app'],ret['data']['control'],ret['data']['act'],ret['data']['id']);
				
				}.bindAsEventListener(this)
			});
		return false;
	},	
	show_reply:function(div_id){
		$(div_id).style.display = 'block';
	},
	hidden_reply:function(div_id){
		$(div_id).style.display = 'none';
	},
	comment_smile:function(content){
		//alert(content);
		var regex = '/\[\/s\:([\d]+)\:\/\]/g';
		//var regex = /[a-z](\d)[a-z]/g;
		var content = content.replace(regex, '<img src="/res/comment/images/smiles/em$1.gif">');
		document.write(content);
	},
	onkeypress:function(id,time_str){
		
		return function (id,time_str){
			alert(id);
		}
	}
});

var smiles = Class.create();
Object.extend(smiles, {
	hide:false,
	show_smile:function(id){
			$(id).blur();
			if(smiles.hide){
				smiles.hide = false;
				$(id).style.display = 'none';

			}else{
				smiles.hide = true;
				$(id).style.display = 'block';
				$(id).focus();

			}
		},
	chick_face:function(id,tag){
		var connent = $('content_'+id).value;
		    connent = connent+tag;
		    $('content_'+id).value = connent;
			$('smile_'+id).style.display = 'none';
	},
	hide_smile:function(id){ //alert(id);
			//smiles.hide = false;
			$(id).style.display = 'none';
		//}

	},
	show_smile_index:function(event,id){
			event = $E(event);
			var src = Event.element(event);
			var dem = src.getDimensions();
			var offset = src.cumulativeOffset();
			var t = new Template(Gift.template);
			$('smile').absolutize();
			$('smile').setStyle({
				top: dem.height - 20 + 'px',
				left: 50 + 'px'
			});
		//	$(id).style.display='none';

	}

});

